package com.atjg.autocat.mqtt;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;

/* loaded from: classes4.dex */
public class MqttManager {
    private static MqttClient client;
    private static MqttManager mInstance = null;
    private MqttConnectOptions conOpt;
    public Handler handler;
    public MqttCallback mCallback;

    public static void printinfo(String info) {
        System.out.println(info);
    }

    private MqttManager() {
        printinfo("=====getInstance==绑定客户端" + this.mCallback);
        Message msg = new Message();
        Object[] ars = {"ddd", "dd"};
        msg.obj = ars;
        printinfo("========MqttManager开始发送消息==========" + msg);
    }

    private MqttManager(Handler h) {
        this.handler = h;
        printinfo("=====getInstance==绑定客户端" + this.mCallback);
        Message msg = new Message();
        Object[] ars = {"ddd", "dd"};
        msg.obj = ars;
        printinfo("========MqttManager开始发送消息==========" + msg);
    }

    public static MqttManager getInstance() {
        printinfo("===============getInstance================绑定客户端");
        if (mInstance == null) {
            synchronized (MqttManager.class) {
                if (mInstance == null) {
                    mInstance = new MqttManager();
                }
            }
        }
        return mInstance;
    }

    public static void release() {
        try {
            if (mInstance != null) {
                disConnect();
                mInstance = null;
            }
        } catch (Exception e) {
            Log.e("MqttManager", "release : " + e.toString());
        }
    }

    public void creatConnect(String brokerUrl, String userName, String password, String clientId, String topic) throws IllegalArgumentException {
        String tmpDir = System.getProperty("java.io.tmpdir");
        new MqttDefaultFilePersistence(tmpDir);
        try {
            MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
            this.conOpt = mqttConnectOptions;
            mqttConnectOptions.setMqttVersion(3);
            this.conOpt.setCleanSession(true);
            this.conOpt.setConnectionTimeout(30);
            this.conOpt.setKeepAliveInterval(10);
            if (userName != null) {
                this.conOpt.setUserName(userName);
            }
            if (password != null) {
                this.conOpt.setPassword(password.toCharArray());
            }
            this.conOpt.setWill(topic, "close".getBytes(), 0, false);
            this.conOpt.setAutomaticReconnect(true);
            MqttClient mqttClient = new MqttClient(brokerUrl, clientId, null);
            client = mqttClient;
            mqttClient.setCallback(this.mCallback);
            doConnect();
        } catch (MqttException e) {
            Log.e("MqttManager", "creatConnect : " + e.toString());
        }
    }

    public void reconnect() {
        MqttClient mqttClient = client;
        if (mqttClient != null) {
            try {
                mqttClient.connect(this.conOpt);
                client.setCallback(this.mCallback);
                Log.e("MqttManager", "reconnect : 重新連接成功！");
            } catch (Exception e) {
                Log.e("MqttManager", "reconnect : " + e.toString());
            }
        }
    }

    public void doConnect() {
        MqttClient mqttClient = client;
        if (mqttClient != null) {
            try {
                mqttClient.connect(this.conOpt);
            } catch (Exception e) {
                Log.e("MqttManager", "doConnect : " + e.toString());
            }
        }
    }

    public void publish(String topicName, int qos, byte[] payload) throws IllegalStateException {
        printinfo("发送publish====" + client.isConnected() + "===========>:" + topicName);
        MqttClient mqttClient = client;
        if (mqttClient != null && mqttClient.isConnected()) {
            MqttMessage message = new MqttMessage(payload);
            message.setPayload(payload);
            message.setQos(qos);
            try {
                client.publish(topicName, message);
                return;
            } catch (MqttException e) {
                printinfo("发送出现异常:" + e.toString());
                Log.e("MqttManager", "publish : " + e.toString());
                return;
            }
        }
        doConnect();
    }

    public void publish(String topicName, int qos, String payload) throws IllegalStateException {
        MqttClient mqttClient = client;
        if (mqttClient != null && mqttClient.isConnected()) {
            MqttMessage message = new MqttMessage(payload.getBytes());
            message.setPayload(payload.getBytes());
            message.setQos(qos);
            try {
                client.publish(topicName, message);
                return;
            } catch (MqttException e) {
                Log.e("MqttManager", "publish : " + e.toString());
                return;
            }
        }
        doConnect();
    }

    public void subscribe(String topicName, int qos) {
        MqttClient mqttClient = client;
        if (mqttClient != null && mqttClient.isConnected()) {
            try {
                client.subscribe(topicName, qos);
            } catch (MqttException e) {
                Log.e("MqttManager", "subscribe : " + e.toString());
            }
        }
    }

    public static void disConnect() throws MqttException {
        MqttClient mqttClient = client;
        if (mqttClient != null && mqttClient.isConnected()) {
            client.disconnect();
        }
        Log.e("MqttManager", "mqtt断开连接");
    }

    public static boolean isConnected() {
        MqttClient mqttClient = client;
        return mqttClient != null && mqttClient.isConnected();
    }
}